package cn.gx.kevin.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.FieldInfo;
import com.alibaba.fastjson.util.TypeUtils;
import cn.gx.kevin.common.ui.ClientButton;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @param <T>
 * @author kevin.huang
 * @ClassName: PagingResult
 * @Description: TODO
 * @date 2016年4月4日 下午5:02:58
 */
public class PagingResult<T> {

    private int currentPage;

    private String dateTimeFormat;
    private long totalSize;

    public void setDateTimeFormat(String dateTimeFormat) {
        this.dateTimeFormat = dateTimeFormat;
    }

    private List<T> resultList = new ArrayList<T>();

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }

    public List<T> getResultList() {
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    public long getPageCount(int pageSize) {
        Double r = Math.ceil(this.totalSize / pageSize);
        int count = r.intValue();
        if (count == 0) {
            return 1;
        }
        return count;
    }

    /**
     * controll 可以为 null
     * buttons 可以为 null
     **/
    public JSONObject toJSONObject(HttpServletRequest request, Class<T> clazz, Set<String> excludeFields,
                                   IControlCommon<T> controll, List<ClientButton> buttons)
            throws Exception {
        if (dateTimeFormat == null) {
            dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("currentPage", getCurrentPage());
        jsonObj.put("totalSize", getTotalSize());
        JSONArray jsonArray = new JSONArray();
        List<FieldInfo> getters = TypeUtils.computeGetters(clazz, null);
        int size = getters.size();
        for (T obj : getResultList()) {
            JSONObject json = new JSONObject(size);
            for (FieldInfo field : getters) {
                String fieldName = field.name;
                if (excludeFields == null || !excludeFields.contains(fieldName)) {
                    Object value = null;
                    if (controll != null) {
                        value = controll.onFieldToJson(obj, fieldName, json);
                    }
                    if (value == null) {
                        value = field.get(obj);
                        if (field.fieldType.getTypeName().equals("java.util.Date")) {
                            Date dateVal = (Date) value;
                            String jsonValue = DateUtils.formatDateTime(dateVal, dateTimeFormat);
                            json.put(fieldName, jsonValue);
                        } else {
                            Object jsonValue = JSONObject.toJSON(value);
                            json.put(fieldName, jsonValue);
                        }
                    } else {
                        json.put(fieldName, value);
                    }
                }
            }
            if (controll != null) {
                controll.extGridResult(obj, json);
            }
            // 生成按钮
            if (buttons != null && buttons.size() > 0) {
                JSONArray btnJsonArr = new JSONArray();
                for (ClientButton btn : buttons) {
                    /*
                     * if(controll.hasDataPrivilage(obj, request, btn.getCmd())){
                     * btn.setDisabled(false); controll.buttonCreateHanlder(obj, btn);
                     * btn.setParams("privilage", "1"); }else{ btn.setParams("privilage", "0");
                     * btn.setDisabled(true); }
                     */
                    btnJsonArr.add(btn.copy().toJSONObject(request, controll, obj));
                }
                json.put("toolbar", btnJsonArr);
            }
            jsonArray.add(json);
        }
        jsonObj.put("resultList", jsonArray);
        return jsonObj;
    }
}
